"use client";

import type { DictType } from "@/lib/api/system/dict.d";
import { deleteDictTypeApi, getDictTypesApi } from "@/lib/api/system/dict";
import {
  ActionType,
  PageContainer,
  ProColumns,
  ProTable,
  ProTableProps,
} from "@ant-design/pro-components";
import { Button, message, Popconfirm, Space } from "antd";
import { useRouter } from "next/navigation";
import { useRequest } from "ahooks";
import { useRef } from "react";
import Link from "next/link";

export default function Dict() {
  const router = useRouter();
  const actionRef = useRef<ActionType>();

  const { run: deleteDictType } = useRequest(deleteDictTypeApi, {
    manual: true,
    onSuccess: () => {
      message.success("删除成功");
      actionRef.current?.reload();
    },
    onError: () => {
      message.error("删除失败");
    },
  });

  const colums: ProColumns<DictType>[] = [
    {
      title: "字典名称",
      dataIndex: "dictName",
      key: "dictName",
    },
    {
      title: "字典类型",
      dataIndex: "dictType",
      key: "dictType",
      render: (_, record) => {
        return (
          <Link href={`/system/dict/data?dictType=${record.dictType}`}>
            {record.dictType}
          </Link>
        );
      },
    },
    {
      title: "备注",
      dataIndex: "remark",
      search: false,
      key: "remark",
    },
    {
      title: "创建时间",
      dataIndex: "createTime",
      key: "createTime",
      valueType: "dateTime",
      width: 150,
      hideInSearch: true,
    },
    {
      title: "创建时间",
      dataIndex: "createdTime",
      valueType: "dateRange",
      hideInTable: true,
      search: {
        transform: (value) => {
          return {
            "params[beginTime]": value[0] + " 00:00:00",
            "params[endTime]": value[1] + " 23:59:59",
          };
        },
      },
    },
    {
      title: "操作",
      key: "action",
      search: false,
      width: 100,
      render: (_, record) => (
        <Space>
          <Button type="link" onClick={() => editHandler(record.dictId)}>
            修改
          </Button>
          <Popconfirm
            title="确定删除吗？"
            description="删除后将无法恢复！"
            onConfirm={() => {
              deleteDictType(record.dictId);
            }}
          >
            <Button type="link" danger>
              删除
            </Button>
          </Popconfirm>
        </Space>
      ),
    },
  ];

  const proTableProps: ProTableProps<DictType, any> = {
    columns: colums,
    rowKey: "dictId",
    pagination: {
      pageSize: 10,
    },
    actionRef: actionRef,
    options: {
      fullScreen: true,
    },
    toolBarRender: () => {
      return [
        <Button onClick={addHandler} key="add" type="primary">
          新增
        </Button>,
      ];
    },
    request: async (params) => {
      const res = await getDictTypesApi(params);

      return {
        data: res.rows,
        success: true,
        total: res.total,
      };
    },
    onRequestError(e) {
      message.error(e.message);
    },
  };

  const addHandler = () => {
    router.push("dict/add");
  };

  const editHandler = (dictId: number) => {
    router.push(`/system/dict/${dictId}`);
  };

  return (
    <PageContainer
      header={{
        title: false,
      }}
    >
      <ProTable {...proTableProps}></ProTable>
    </PageContainer>
  );
}
